<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller
{
    public function index()
    {
        echo 2534345;
    }

    /**
     * 加载前台FD商城主页
     */
    public function home()
    {
        //生成商品分类树
        $classification = D('Classification');
        $classification_info = $classification->getTreeData('level','id','classification');
        $info = $classification_info;
        foreach($info as $k=>$v){
            //删除多余字段
            unset($info[$k]['pid'],$info[$k]['add_time'],$info[$k]['_level'],$info[$k]['_html']);

            $goods = D('Goods')->where(['class_id'=>$v['id']])->getField('id,goods_name,litimg,fd_price');
            if($goods){
                $info[$k]['_goods'] = $goods;
            }else{
                $info[$k]['_goods'] = array();
            }
            foreach($v['_data'] as $kk=>$vv){
                if(count($info[$k]['_goods'],0) > 6){
                    break;
                }
                unset($info[$k]['_data'][$kk]['pid'],$info[$k]['_data'][$kk]['add_time'],$info[$k]['_data'][$kk]['_level'],$info[$k]['_data'][$kk]['_html'],$info[$k]['_data'][$kk]['_data']);
                $goods = D('Goods')->where(['class_id'=>$vv['id']])->getField('id,goods_name,class_id,litimg,fd_price');
                if($goods){
                    $info[$k]['_goods'] = array_merge($info[$k]['_goods'],$goods);
                }
                foreach($vv['_data'] as $kkk=>$vvv){
                    if(count($info[$k]['_goods'],0) > 6){
                        break;
                    }
                    $goods = D('Goods')->where(['class_id'=>$vvv['id']])->getField('id,goods_name,class_id,litimg,fd_price');
                    if($goods){
                        $info[$k]['_goods'] = array_merge($info[$k]['_goods'],$goods);
                    }
                }
            }
        }
        //banner图获取
        $banner_images = M('Ad')->where(['type'=>1,'status'=>1,])->field('litimg,link_url')->order('sort_order asc')->select();
        //加惊商品获取
        $surprise_goods = D('Goods')->get_many('is_integral=0 and is_sale=1 and is_surprise=1','id,goods_name,market_price,fd_price,litimg','','4');
        //加精商品获取
        $boutique_goods = D('Goods')->get_many('is_integral=0 and is_sale=1 and is_boutique=1','id,goods_name,fd_price,litimg,description','','4');
        //新品获取
        $new_goods = D('Goods')->get_many('is_integral=0 and is_sale=1','id,goods_name,market_price,fd_price,litimg,description','add_time desc','2');
        $this->assign('banner_images',$banner_images);
        $this->assign('classification_info',$classification_info);
        $this->assign('surprise_goods',$surprise_goods);
        $this->assign('boutique_goods',$boutique_goods);
        $this->assign('new_goods',$new_goods);
        $this->assign('info',$info);
        $this->assign('title','FD商城首页');
        $this->display();
    }
}